import os
import shutil
from os.path import exists

def save_sen2txt(sentences_list):
    """
    将nltk分句后的句子list以txt文件格式存储到predict_output文件夹中。
    加到utils里面。
    """
    predict_output_dir = "static"
    sen_path = os.path.join(predict_output_dir, "sentences.txt")
    f = open(sen_path, 'w', encoding='utf-8')
    for sentence_index in range(len(sentences_list)):
        f.write(str(sentence_index) + " " + sentences_list[sentence_index] + "\n")
    f.close()


"""
下面是刚才为了存储best句子位置，main里面要加的内容：

        pos_path = predict_output_dir + "position.txt"
        f = open(pos_path, 'w', encoding='utf - 8')
        for sentence_index in range(args.num):
            f.write(str(sentence_index) + " " + str(position[sentence_index]) + "\n")
        f.close()
"""


def save_positions2txt(position_list):
    print("writing positon begin")
    predict_output_dir = "static"
    sen_path = os.path.join(predict_output_dir, "positons.txt")
    print("open begin")
    f = open(sen_path, 'w', encoding='utf-8')
    print("opened")
    print(len(position_list))
    for sentence_index in range(len(position_list)):
        print("writeposition")
        f.write(str(position_list[sentence_index]) + "\n")
    f.close()


def save_sen2corpus_image(filenames):
    src = []
    predict_output_dir = "static"
    for i in range(len(filenames)):
        corpus_image_path = os.path.join("data/train/image", filenames[i])
        shutil.copy(corpus_image_path, predict_output_dir)
        new_path = os.path.join(predict_output_dir, (str(i) + ".jpg"))
        if os.path.exists(new_path):
            os.remove(new_path)
        old_name = os.path.join(predict_output_dir, filenames[i])
        os.rename(old_name, new_path)
